package com.sc.site.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.member.MemberAddress;
import com.sc.service.member.MemberAddressService;
import com.sc.service.member.RegionService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
/**
 * 
 * <h1>会员地址信息管理</h1> 
 * 
 * @author xujunbao 
 * @version AppMemberAddressController.java,v0.1 2017年3月9日 上午11:14:29 xujunbao Exp  
 *
 */
@RequestMapping("/address")
@Controller
public class MemberAddressController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private MemberAddressService memberAddressService;
	@Autowired
	private RegionService regionService;
	
	/**
	 * 使用新地址
	 * @return
	 */
	@RequestMapping(value="/add_address")
	public String add_address(){
		return "/order/add_address";
	}
	
	/**
	 * 管理收货地址页面
	 * @return
	 */
	@RequestMapping(value="/manage_addr")
	public String manage_addr(){
		return "/order/manage_addr";
	}
	
	@RequestMapping("selectBymId")
	@ResponseBody
	public Map<String, Object> selectBymId(String mId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			List<MemberAddress> addressList=memberAddressService.selectBymId(mId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data",addressList);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据用户id查询订单信息异常"+mId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 
	 * <h2>根据地址主键查询用户地址</h2> 
	 * 
	 * @param mId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/selectAddressById")
	@ResponseBody
	public Map<String, Object> selectAddressById(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress memberAddress=memberAddressService.selectAddressById(maId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data", memberAddress);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id查询地址信息异常"+maId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 查询用户的默认地址
	 * <h2>方法注释说明</h2> 
	 * 
	 * @param mId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/selectDefaultAddress")
	@ResponseBody
	public Map<String, Object> selectDefaultAddress(String mId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress memberAddress=memberAddressService.selectDefaultAddress(mId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			resultMap.put("data", memberAddress);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id查询地址信息异常"+mId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			return resultMap;
		}
	}
	/**
	 * 
	 * <h2>修改常用地址信息</h2> 
	 * 
	 * @param maId
	 * @param name
	 * @param tel
	 * @param province
	 * @param city
	 * @param county
	 * @param street
	 * @param isdefault
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/addOrUpdateAddress")
	@ResponseBody
	public Map<String,Object> addOrUpdateAddress(MemberAddress memberAddress){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			memberAddressService.addOrUpdateAddressById(memberAddress);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SAVE);
			return resultMap;
		}
	}
	/**
	 * 根据地址id修改默认地址
	 * <h2>方法注释说明</h2> 
	 * 
	 * @param maId
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping(value="/setDefaultAddress")
	@ResponseBody
	public Map<String, Object> setDefaultAddress(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			MemberAddress address=memberAddressService.selectByPrimaryKey(maId);
			address.setIsdefault("1");
			memberAddressService.addOrUpdateAddressById(address);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
		}
		return resultMap;
	}
	/**
	 * 
	 * <h2>返回地区信息</h2> 
	 * 
	 * @return
	 * @return List<Region> 
	 * @throws
	 */
	@RequestMapping("/selectAllRegion")
	@ResponseBody
	public Map<String, Object> selectAllRegion(){ 
		Map<String, Object> resultMap=new HashMap<String, Object>();
		List<?> regionList=memberAddressService.selectAllRegionPc();
		DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		resultMap.put("citylist",regionList );
		DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, regionList);
		return  resultMap;
	}
	/**
	 * 
	 * <h2>根据id删除地址信息</h2> 
	 * 
	 * @param maId 地址的唯一id
	 * @return
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/deleteAddressById")
	@ResponseBody
	public Map<String, Object> deleteAddressById(String maId){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			memberAddressService.deleteAddressById(maId);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			return resultMap;
		} catch (Exception e) {
			logger.error("根据地址id删除地址信息异常"+maId, e);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_DELETE);
			return resultMap;
		}
	}

}
